import { AppRegistry, BackHandler, ToastAndroid } from 'react-native'
import { StackNavigator, TabNavigator, TabBarBottom } from 'react-navigation'

import React, { Component } from 'react'
import axios from 'axios'
import GetSetStorge from './src/common/GetSetStorge'
import { configRoute, RouteHelper } from 'react-navigation-easy-helper'
import { needLoginPage } from './src/common/needLoginPage'

import TabBarItem from './src/common/TabBarItem'
import Splash from './Splash'
import Home from './src/view/Home/Home'
import Qrcode from './src/view/Home/Qrcode'
import TicketVerify from './src/view/Home/TicketVerify'
import SellTicket from './src/view/Home/SellTicket'
import PaySuccess from './src/view/Home/PaySuccess'


import Ticketing from './src/view/Ticketing/Ticketing'
import Inspect from './src/view/Inspect/Inspect'
import InspectQuery from './src/view/Inspect/InspectQuery'
import InspectItem from './src/view/Inspect/InspectItem'
import Print from './src/view/Home/Print'
import RefundTicket from './src/view/RefundTicket/RefundTicket'


let routes = [];
let lastBackPressed = null
class Index extends Component {

	async getToken() {
		return await GetSetStorge.getStorgeAsync(`token`)
	}

	async axiosFilter() {
		// token统一配置
		axios.interceptors.request.use(
			config =>
				this.config(config)
			, function (error) {
				return Promise.reject(error)
			}
		)
	}

	async config(config) {
		let token = await this.getToken()
		config.headers['token'] = token
		return config
	}

	async loginConfig(routeName, params) {
		let token = await this.getToken()
		if (!token && needLoginPage.indexOf(routeName) !== -1) {
			RouteHelper.push('Login', {
				successCallBack: () => {
					RouteHelper.push(routeName, params)
				}
			})
			// alert('跳登陆'+ routeName)
			return false
		}
		return true
	}

	async pageFilter() {
		//跳转拦截器用法
			RouteHelper.routeInterceptor = (routeName, params) => this.loginConfig(routeName, params)
	}

	componentDidMount() {
		this.axiosFilter()
		// this.pageFilter()

		BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid)
	}

	componentWillUnmount() {
		BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid)
		lastBackPressed = null
	}

	onBackAndroid() {
		if (routes.length === 2) { // 根界面
			if (lastBackPressed && lastBackPressed + 2000 >= Date.now()) {
				BackHandler.exitApp()
				return false
			}
			lastBackPressed = Date.now()
			ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT)
			return true
		}
	}

	render() {
		return (
			<RootNavigator
				onNavigationStateChange={(prevNav, nav, action) => {
					console.log('prevNav=', prevNav)
					console.log('nav=', nav)
					console.log('action=', action)
					routes = nav.routes
				}}
			/>
		)
	}
}

export default Index




// const Tab = TabNavigator({})

// const Tab = TabNavigator(
// 	{
// 		Home: {
// 			screen: Home,
// 			navigationOptions: ({ navigation }) => ({
// 				tabBarLabel: '售票',
// 				tabBarIcon: ({ focused, tintColor }) => (
// 					<TabBarItem
// 						tintColor={tintColor}
// 						focused={focused}
// 						normalImage={require('./src/img/common/home.png')}
// 						selectedImage={require('./src/img/common/home_selected.png')}
// 					/>
// 				)
// 			}),
// 		},
// 		Inspect: {
// 			screen: Inspect,
// 			navigationOptions: ({ navigation }) => ({
// 				tabBarLabel: '验票',
// 				tabBarIcon: ({ focused, tintColor }) => (
// 					<TabBarItem
// 						tintColor={tintColor}
// 						focused={focused}
// 						normalImage={require('./src/img/common/travel.png')}
// 						selectedImage={require('./src/img/common/travel_selected.png')}
// 					/>
// 				)
// 			}),
// 		},

// 		Ticketing: {
// 			screen: Ticketing,
// 			navigationOptions: ({ navigation }) => ({
// 				tabBarLabel: '票务记录',
// 				tabBarIcon: ({ focused, tintColor }) => (
// 					<TabBarItem
// 						tintColor={tintColor}
// 						focused={focused}
// 						normalImage={require('./src/img/common/customer.png')}
// 						selectedImage={require('./src/img/common/customer_selected.png')}
// 					/>
// 				)
// 			}),
// 		},

// 	},
// 	{
// 		tabBarComponent: TabBarBottom,
// 		tabBarPosition: 'bottom',
// 		lazy: false,
// 		animationEnabled: false,
// 		swipeEnabled: false,
// 		tabBarOptions: {
// 			// activeTintColor: '#ff0',
// 			inactiveTintColor: '#979797',
// 			style: {
// 				backgroundColor: '#ffffff',
// 				height: 50,
// 			},
// 			labelStyle: {
// 				fontSize: 11,
// 				// marginBottom: 4
// 			}
// 		},
// 	}

// )

/*
 * 初始化StackNavigator
 */
const RootNavigator = StackNavigator(
	configRoute({
		//默认加载第一个页面，这里用来注册需要跳转的页面 相当于Manifest.xml文件
		Splash: {
			screen: Splash,
			navigationOptions: {
				header: null,
				gesturesEnabled: false // 是否允许右滑返回，在iOS上默认为true，在Android上默认为false
			}
		},
		App: {
			screen: Home,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
        },
		TicketVerify: {
			screen: TicketVerify,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
        SellTicket: {
			screen: SellTicket,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		PaySuccess: {
			screen: PaySuccess,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		Qrcode: {
			screen: Qrcode,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		Ticketing: {
			screen: Ticketing,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		Inspect: {
			screen: Inspect,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		InspectQuery: {
			screen: InspectQuery,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		InspectItem: {
			screen: InspectItem,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		Print: {
			screen: Print,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		},
		RefundTicket: {
			screen: RefundTicket,
			navigationOptions: {
				header: null,
				gesturesEnabled: false
			}
		}
	})
)


console.disableYellowBox = true // 去除警告
AppRegistry.registerComponent('sjhh', () => Index);
